#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include <aos/aos.h>
#include <aos/yloop.h>
#include "netmgr.h"
#include "aos/kv.h"
#include "iot_import.h"
//
#include "hal/soc/soc.h"

#include "hal/soc/uart.h"

//#include "mxos.h"


#include "lj.h"

#include "lj_proto_usart.h"
//#include "lj_i2c.h"

//#include "platform_peripheral.h"//spp 3080
//#include "iot_export_linkkit.h"
//#include "mkv.h"

//#define LOG(fmt, ...)  custom_log("zzc", fmt, ##__VA_ARGS__)

#define ZZC_LOG LOG


#define WDG_PORT_NUM 0

wdg_dev_t lj_wdg;

extern void do_awss_active(void);
extern void do_awss_reset(void);
extern int lj_user_post_raw_data(unsigned char *buf,unsigned short len);

//void lj_set(unsigned char *buf,unsigned char len);

void ljhf_set_dis(unsigned char buf);
void ljhf_delayms(unsigned short time)
{
	unsigned short i = 0, j = 0;
	for(i = 0; i < time; i++){
		for(j = 0; j < 1000; j++){
			//os_delay_us(1);
		}
	}
}



extern void on_awss_key_clicked(void);
extern void on_awss_key_long_clicked(void);

//#include "linkkit_cli.h"

#define LJ_CMD_MAX 5
#define LJ_CMD_LEN_MAX 16
char lj_cmd[LJ_CMD_MAX][LJ_CMD_LEN_MAX]={0};

#define LINKKIT_AWSS_MODE_KV "linkkit_awss_mode"

void set_ble_awss(void)
{
//	strcpy(lj_cmd[0],"awss");
//	strcpy(lj_cmd[1],"ble");
//	handle_awss_cmd(2,lj_cmd);

//	linkkit_awss_mode_set(2);

	unsigned char mode=0;

	int vlen = sizeof(mode);

//	mkv_item_set( LINKKIT_AWSS_MODE_KV, &mode, vlen );
}


void set_sta_awss(void)
{
//	strcpy(lj_cmd[0],"awss");
//		strcpy(lj_cmd[1],"start");
//	handle_awss_cmd(2,lj_cmd);
//	linkkit_awss_mode_set(0);

	unsigned char mode=2;

		int vlen = sizeof(mode);
//		mkv_item_set( LINKKIT_AWSS_MODE_KV, &mode, vlen );
}



void ljhf_reset_sdk(void)
{
//	mkv_item_delete("city");
//	on_awss_key_long_clicked();
//	lj_report_unbind();
    do_awss_reset();

//	set_ble_awss();
//	linkkit_reset();

}



void ljhf_smart_link(void)
{
  //  do_awss_active();
#ifdef _04_reset_
//	set_sta_awss();
//	linkkit_reset();
	do_awss_reset();
//		set_ble_awss();
//	on_awss_key_long_clicked();
#else
	ljhf_reset_sdk();
//	on_awss_key_clicked();
#endif
}

unsigned char ljhf_ask_net(unsigned char* wifi,unsigned char *net)
{
	*wifi=all_wifi;
	*net=all_net;
//	ZZC_LOG("zzc........get int to ask net\r\n");
	return 1;
}

unsigned char time_restart=0;

void restart_init(void)
{
	// need_restart=1;
	time_restart=20;
}

void restart_loop(void)
{
	if(time_restart)
	{
		time_restart--;
		if(time_restart==0)
		{
//			mxos_system_power_perform(NULL, eState_Software_Reset);
			HAL_Reboot();
		}
	}
}

unsigned char ljhf_restart(void)
{
	restart_init();

	return 1;
}

//unsigned char ljhf_restart(void)
//{
////	reboot_system(NULL);
//	 mico_system_power_perform(NULL, eState_Software_Reset);
//	return 1;
//}

unsigned char ljhf_reload(void)
{
//	hfsys_reload();
//	netmgr_clear_ap_config();
	netmgr_clear_ap_config();
	return 1;
}

unsigned char ljhf_get_mac(unsigned char *mac_buf)
{
	lj_int ret;
//	mwifi_get_mac( mac_buf );
    hal_wifi_get_mac_addr(NULL, mac_buf);
    return 0;
}

#include "netmgr.h"
#define SSID_MAX 33
unsigned char ljhf_get_ssid_pwd(char * buf)
{
	unsigned int i=0;
	char ssid[SSID_MAX]={0},pwd[SSID_MAX*2]={0},doc[2]=",",buff[SSID_MAX*2+3]={0};
	netmgr_ap_config_t config;
	ZZC_LOG("zzc...............zzc_get_ssid_pwd");

    memset(&config, 0, sizeof(netmgr_ap_config_t));
    if(netmgr_get_ap_config(&config)==-1)
    {
        LOG("lj_netmgr_get_ap_config.................=-1");
		return 0;
    }

	strncpy(ssid,config.ssid,sizeof(config.ssid) - 1);
	strncpy(pwd,config.pwd,sizeof(config.pwd) - 1);
	strcpy(buff,ssid);
	strcat(buff,doc);
	strcat(buff,pwd);
	for(i=0;i<strlen(buff)+1;i++)
	{
		buf[i]=buff[i];
	}
	ZZC_LOG("zzc...............zzc_get_ssid_pwd=%s,strlen is %d\r\n",buf,strlen(buf));
//	buf[0]='z';
//	buf[1]='z';
//	buf[2]='c';
//	buf[3]=0;
	if(strlen(buf)==0)
	{
		return 0;
	}
	return 1;
}

unsigned char ljhf_set_ssid_pwd(char * buf)
{
	return 0;
}




unsigned char ljhf_get_idkey(unsigned char * buf,unsigned int *len)
{
	unsigned int i=0,j=0;
	int len_pk=0,len_ps=0,len_dn=0,len_ds=0;
	char pidStr[9] = { 0 };
	int len_id=0;
	int ret=0;

	char pk[LJ_PRODUCT_KEY_MAXLEN + 1] = {0};
	char ps[LJ_DEVICE_SECRET_MAXLEN + 1] = {0};
	char dn[LJ_DEVICE_NAME_MAXLEN + 1] = {0};
	char ds[LJ_DEVICE_SECRET_MAXLEN + 1] = {0};

	len_pk=HAL_GetProductKey(pk);
	len_ps=HAL_GetProductSecret(ps);
	len_dn=HAL_GetDeviceName(dn);
	len_ds=HAL_GetDeviceSecret(ds);
//	int ret = -1;
//	char pidStr[9] = { 0 };
	len_id = sizeof(pidStr);

	ret = aos_kv_get("linkkit_product_id", pidStr, &len_id);
	if (ret == 0 && len_id < sizeof(pidStr)) {
//		*pid = atoi(pidStr);
	} else {
		ret = 0;
	}


	if(!((len_pk>=0)&&(len_ps>=0)&&(len_dn>=0)&&(len_ds>=0)))
	{
		return 0;
	}
	j=0;
	for(i=0;i<len_pk;i++)
	{
		buf[i+j]=pk[i];
	}
	buf[i+j]=',';
	j=i+j+1;
	for(i=0;i<len_ps;i++)
	{
		buf[i+j]=ps[i];
	}
	buf[i+j]=',';
	j=i+j+1;
	for(i=0;i<len_dn;i++)
	{
		buf[i+j]=dn[i];
	}
	buf[i+j]=',';
	j=i+j+1;
	for(i=0;i<len_ds;i++)
	{
		buf[i+j]=ds[i];
	}
	buf[i+j]=',';
	j=i+j+1;
	for(i=0;i<strlen(pidStr);i++)
	{
		buf[i+j]=pidStr[i];
	}
	j=i+j;
	*len=j;



	return 1;
}


unsigned char ljhf_set_idkey(lj_uchar *buf,unsigned short buf_len)
{
	unsigned int i=0,j=0,len[5]={0};
	int ret=0;

	char pk[LJ_PRODUCT_KEY_MAXLEN +1] = {0};
	char ps[LJ_DEVICE_SECRET_MAXLEN+1] = {0};
	char dn[LJ_DEVICE_NAME_MAXLEN+1] = {0};
	char ds[LJ_DEVICE_SECRET_MAXLEN+1] = {0};
	char id[LJ_DEVICE_ID_MAXLEN]={0};
	if((buf_len==3)&&(buf[0]==0x55)&&(buf[1]==0x66)&&(buf[2]==0x77))
	{
//		ret=mkv_item_delete("linkkit_product_key");
//		ret=mkv_item_delete("linkkit_device_name");
//		ret=mkv_item_delete("linkkit_device_secret");
//		ret=mkv_item_delete("linkkit_product_secret");
//		ret=mkv_item_delete("linkkit_product_id");
		aos_kv_del("linkkit_product_key");
		aos_kv_del("linkkit_device_name");
		aos_kv_del("linkkit_device_secret");
		aos_kv_del("linkkit_product_secret");
		aos_kv_del("linkkit_product_id");
		ZZC_LOG("get in to del linkkit pk dn ds ps id  ret=%d",ret);
		return 1;
	}
	for(i=0;i<buf_len;i++)
	{
		if(buf[i]==',')
		{
			len[j]=i;
			j++;
		}
	}
	if((j<3)||(len[0]==0)||(len[1]<=2)||(len[2]<=4)||(len[3]<=1))
	{
		return 0;
	}
	for(i=0;i<len[0];i++)
	{
		pk[i]=buf[i];
	}
	for(i=len[0]+1;i<len[1];i++)
	{
		ps[i-len[0]-1]=buf[i];
	}
	for(i=len[1]+1;i<len[2];i++)
	{
		dn[i-len[1]-1]=buf[i];
	}
	for(i=len[2]+1;i<len[3];i++)
	{
		ds[i-len[2]-1]=buf[i];
	}

	if(j==3)
	{
	}
	else
	{
		for(i=len[3]+1;i<buf_len;i++)
		{
			id[i-len[3]-1]=buf[i];
		}
	}
	ZZC_LOG("len[0]=%d,len[1]=%d,len[2]=%d,len[3]=%d",len[0],len[1],len[2],len[3]);

	 aos_kv_set("linkkit_product_key", pk, LJ_PRODUCT_KEY_MAXLEN + 1,1);
	 aos_kv_set("linkkit_device_name", dn, LJ_DEVICE_NAME_MAXLEN + 1,1);
	 aos_kv_set("linkkit_device_secret", ds, LJ_DEVICE_SECRET_MAXLEN + 1,1);
	 aos_kv_set("linkkit_product_secret", ps, LJ_DEVICE_SECRET_MAXLEN + 1,1);
	 if(j==4)
	 {
		 aos_kv_set("linkkit_product_id", id, strlen(id)+1,1);
//	 mkv_item_set("linkkit_product_id", id, LJ_DEVICE_ID_MAXLEN + 1);
	 }


	HAL_SetProductKey(pk);
	HAL_SetProductSecret(ps);
	HAL_SetDeviceName(dn);
	HAL_SetDeviceSecret(ds);
	if(j==4)
	{

	}
	return 1;

}



unsigned char ljhf_publish(unsigned char * buf,unsigned short buf_len)
{
	return buf_len;
}


#define RAW_BUF_MAX (512)
unsigned char raw_buf[RAW_BUF_MAX]={0};



unsigned char ljhf_publish_point_npush_raw_85(unsigned char *buf,unsigned short buf_len)
{
	unsigned int i=0;
	unsigned int j=0,add_count=0;
	unsigned char check_num=0;
	raw_buf[0]=0xff;
	raw_buf[1]=((buf_len+5)>>8);
	raw_buf[2]=buf_len+5;
	raw_buf[3]=0x85;
	raw_buf[4]=0;

	check_num=check_num^raw_buf[1];
	check_num=check_num^raw_buf[2];
	check_num=check_num^raw_buf[3];
	check_num=check_num^raw_buf[4];
	for(i=0;i<buf_len;i++)
	{
		check_num=check_num^buf[i];
		// if((buf[i]==0xff)||(buf[i]==0xfe)||(buf[i]==0xfd))
		// {
		// 	raw_buf[5+j]=0xfd;
		// 	add_count++;
		// 	j++;
		// 	raw_buf[5+j]=(buf[i]&0x0f);
		// 	j++;
		// }
		// else
//		{
			if(5+j<RAW_BUF_MAX-2)
			{
			raw_buf[5+j]=buf[i];
			j++;
			}
//		}
	}

	raw_buf[5+buf_len+add_count]=check_num;
	raw_buf[5+buf_len+add_count+1]=0xfe;


	up_put_ring(raw_buf,5+buf_len+add_count+2);
	ZZC_LOG("put in up_put_ring len=%d",5+buf_len+add_count+2);
	print_x(raw_buf,5+buf_len+add_count+2);
	return 1;

}


//#if (defined  _chaoren_jhq_ || defined _meizhi_)
#ifdef bigualu


unsigned char ljhf_publish_point_npush_raw(unsigned char *buf,unsigned short buf_len,lj_usart_pkt abuf_st)
{
	unsigned int i=0;
	unsigned int j=0,add_count=0;
	unsigned char check_num=0;
	raw_buf[0]=0xff;
	raw_buf[1]=((buf_len+5)>>8);
	raw_buf[2]=buf_len+5;
	raw_buf[3]=0x85;
	raw_buf[4]=0;

	check_num=check_num^raw_buf[1];
	check_num=check_num^raw_buf[2];
	check_num=check_num^raw_buf[3];
	check_num=check_num^raw_buf[4];
	for(i=0;i<buf_len;i++)
	{
		check_num=check_num^buf[i];
		// if((buf[i]==0xff)||(buf[i]==0xfe)||(buf[i]==0xfd))
		// {
		// 	raw_buf[5+j]=0xfd;
		// 	add_count++;
		// 	j++;
		// 	raw_buf[5+j]=(buf[i]&0x0f);
		// 	j++;
		// }
		// else
		{
			raw_buf[5+j]=buf[i];
			j++;
		}
	}

	raw_buf[5+buf_len+add_count]=check_num;
	raw_buf[5+buf_len+add_count+1]=0xfe;


#ifdef _up_make_out_use_ring_
	up_put_ring(raw_buf,5+buf_len+add_count+2);
	ZZC_LOG("put in up_put_ring len=%d",5+buf_len+add_count+2);
	return 1;
#else
    if(lj_user_post_raw_data(raw_buf,5+buf_len+add_count+2)!=-1)
    {
    	ZZC_LOG("1before HAL_SleepMs");
    	    	HAL_SleepMs(200);
    	    	ZZC_LOG("1after HAL_SleepMs");
	return 1;
    }
    ZZC_LOG("2before HAL_SleepMs");
       HAL_SleepMs(200);
       ZZC_LOG("2after HAL_SleepMs");
    return 0;
#endif
}


#else

#if 0

unsigned char ljhf_publish_point_npush_raw(unsigned char *buf,unsigned short buf_len,lj_usart_pkt abuf_st)
{
	unsigned int i=0;
	raw_buf[0]=0xff;
	raw_buf[1]=abuf_st.len_h;
	raw_buf[2]=abuf_st.len_l;
	raw_buf[3]=abuf_st.cmd;
	raw_buf[4]=abuf_st.r_id;
	for(i=0;i<buf_len;i++)
	{
		raw_buf[5+i]=buf[i];
	}
	raw_buf[abuf_st.len_num]=abuf_st.check_num;
	raw_buf[abuf_st.len_num+1]=0xfe;

#ifdef _up_make_out_use_ring_
	up_put_ring(raw_buf,abuf_st.len_num+2);
	ZZC_LOG("put in up_put_ring len=%d",abuf_st.len_num+2);
	return 1;
#else
	// lj_raw_up(raw_buf,abuf_st.len_num+2);
    if(lj_user_post_raw_data(raw_buf,abuf_st.len_num+2)!=-1)
    {
	return 1;
    }
    return 0;
#endif

#endif //0

unsigned char ljhf_publish_point_npush_raw(unsigned char *buf,unsigned short buf_len,lj_usart_pkt abuf_st)
{
	unsigned int i=0;
	static unsigned char rid=0;
	unsigned int j=0,add_count=0;
	unsigned char check_num=0;
	if(0)
	{
		raw_buf[0]=0xff;
		raw_buf[1]=((buf_len+5)>>8);
		raw_buf[2]=buf_len+5;
		raw_buf[3]=0x85;
		raw_buf[4]=rid;
		rid++;

		check_num=check_num^raw_buf[1];
		check_num=check_num^raw_buf[2];
		check_num=check_num^raw_buf[3];
		check_num=check_num^raw_buf[4];
		for(i=0;i<buf_len;i++)
		{
			check_num=check_num^buf[i];
			// if((buf[i]==0xff)||(buf[i]==0xfe)||(buf[i]==0xfd))
			// {
			// 	raw_buf[5+j]=0xfd;
			// 	add_count++;
			// 	j++;
			// 	raw_buf[5+j]=(buf[i]&0x0f);
			// 	j++;
			// }
			// else
			{
				raw_buf[5+j]=buf[i];
				j++;
			}
		}

		raw_buf[5+buf_len+add_count]=check_num;
		raw_buf[5+buf_len+add_count+1]=0xfe;
		up_put_ring(raw_buf,5+buf_len+add_count+2);
		ZZC_LOG("put in up_put_ring len=%d",5+buf_len+add_count+2);
		return 1;
	}
	else
	{
		raw_buf[0]=0xff;
		raw_buf[1]=abuf_st.len_h;
		raw_buf[2]=abuf_st.len_l;
		raw_buf[3]=abuf_st.cmd;
		raw_buf[4]=abuf_st.r_id;
		for(i=0;i<buf_len;i++)
		{
			raw_buf[5+i]=buf[i];
		}
		raw_buf[abuf_st.len_num]=abuf_st.check_num;
		raw_buf[abuf_st.len_num+1]=0xfe;


		up_put_ring(raw_buf,abuf_st.len_num+2);
		ZZC_LOG("put in up_put_ring len=%d",abuf_st.len_num+2);
		return 1;
	}


}
#endif


unsigned char ljhf_publish_point_npush(unsigned char *buf,unsigned short buf_len)
{
    return 0;
}

unsigned char ljhf_publish_point_xpush(unsigned char *buf,unsigned short buf_len)
{
    return 0;
}

void num_to_string_2(unsigned int num1,char *buf)
{
	sprintf(buf,"%d",num1);
}

unsigned char ljhf_publish_point_push(unsigned char *buf,unsigned short buf_len)
{
    return 0;
}


#ifdef weather_https
#include "lj_https.h"
extern unsigned char need_get_weather;
#endif

void ljhf_unfind(void)
{
#ifdef weather_https
	need_get_weather=1;
#endif
}

void ljhf_unbind(void)
{

}


//void ljhf_set_dis(unsigned char buf)
//{
////	need_get_weather=1;
//}

unsigned char  ljhf_get_id_work(unsigned char * id,unsigned char *key)
{
    return 0;
}

void ljhf_get_ver(unsigned char * buf,unsigned short *len)
{
	char *ver;
	ver=(char *)aos_get_app_version();
	*len=strlen(ver);
	memcpy(buf,ver,*len);
}

unsigned char ljhf_init_point_all(unsigned char * buf ,unsigned short buf_len)
{
    return 0;
}

unsigned int all_timestamp=0;

typedef struct
{
	unsigned char hour;
	unsigned char min;
	unsigned char sec;
	unsigned short w_year;
	unsigned char  w_month;
	unsigned char  w_date;
	unsigned char  week;
}_calendar_obj;

_calendar_obj calendar;

unsigned char Is_Leap_Year(unsigned short year)
{
	if(year%4==0) //±ØÐëÄÜ±»4Õû³ý
	{
		if(year%100==0)
		{
			if(year%400==0)return 1;//Èç¹ûÒÔ00½áÎ²,»¹ÒªÄÜ±»400Õû³ý
			else return 0;
		}else return 1;
	}else return 0;
}

unsigned char const table_week_1[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //鑴剹鑴ㄨ劶鑴獟鑴㈠獟鎴劵鍗ら搯
const unsigned char mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};
unsigned char zzc_RTC_Get_Week(unsigned short year,unsigned char month,unsigned char day)
{
	unsigned short temp2;
	unsigned char yearH,yearL;
	yearH=year/100;	yearL=year%100;
	if (yearH>19)yearL+=100;
	temp2=yearL+yearL/4;
	temp2=temp2%7;
	temp2=temp2+day+table_week_1[month-1];
	if (yearL%4==0&&month<3)temp2--;
	return(temp2%7);
}

//void  zzc_sntp_get_real_time(unsigned int  timestamp,lj_time_t * time_now)
//{
//	struct tm tm_now;
//	int timezone = 8;
//	static char time[64];
////	lj_time_t time_now;
//	tm_now = *(localtime(&timestamp));
//	tm_now.tm_year += 1900;
//	tm_now.tm_mon += 1;
//
//	time_now->area=timezone;
//	time_now->year=tm_now.tm_year;
//	time_now->month=tm_now.tm_mon;
//	time_now->day=tm_now.tm_mday;
//	time_now->hour=tm_now.tm_hour;
//	time_now->minute=tm_now.tm_min;
//	time_now->second=tm_now.tm_sec;
//	time_now->week=zzc_RTC_Get_Week(time_now->year,time_now->month,time_now->day);
//
//	sprintf(time, "zzc..%d-%d-%d %d:%d:%d UTC%s%d\n",
//		tm_now.tm_year, tm_now.tm_mon, tm_now.tm_mday, tm_now.tm_hour, tm_now.tm_min, tm_now.tm_sec,
//		(timezone > 0) ? "+" : "", timezone);
//	printf("date:%s\r\n",time);
////	return &time_now;
//}

unsigned char RTC_Get(unsigned int timecount)//,lj_time_t *time_t)
{
	static unsigned short daycnt=0;

	unsigned int temp=0;
	unsigned short temp1=0;


 	temp=(timecount)/86400;   //µÃµ½ÌìÊý(ÃëÖÓÊý¶ÔÓ¦µÄ)
	if(daycnt!=temp)//³¬¹ýÒ»ÌìÁË
	{
		daycnt=temp;
		temp1=1970;	//´Ó1970Äê¿ªÊ¼
		while(temp>=365)
		{
			if(Is_Leap_Year(temp1))//ÊÇÈòÄê
			{
				if(temp>=366)temp-=366;//ÈòÄêµÄÃëÖÓÊý
				else break;
			}
			else temp-=365;	  //Æ½Äê
			temp1++;
		}
		calendar.w_year=temp1;//µÃµ½Äê·Ý
		temp1=0;
		while(temp>=28)//³¬¹ýÁËÒ»¸öÔÂ
		{
			if(Is_Leap_Year(calendar.w_year)&&temp1==1)//µ±ÄêÊÇ²»ÊÇÈòÄê/2ÔÂ·Ý
			{
				if(temp>=29)temp-=29;//ÈòÄêµÄÃëÖÓÊý
				else break;
			}
			else
			{
				if(temp>=mon_table[temp1])temp-=mon_table[temp1];//Æ½Äê
				else break;
			}
			temp1++;
		}
		calendar.w_month=temp1+1;	//µÃµ½ÔÂ·Ý
		calendar.w_date=temp+1;  	//µÃµ½ÈÕÆÚ
	}
	temp=timecount%86400;     		//µÃµ½ÃëÖÓÊý
	calendar.hour=temp/3600;     	//Ð¡Ê±
	//calendar.hour=calendar.hour;//+8;
	if(calendar.hour>=24)
	{
		calendar.hour=calendar.hour-24;;
	}
	calendar.min=(temp%3600)/60; 	//·ÖÖÓ
	calendar.sec=(temp%3600)%60; 	//ÃëÖÓ
	calendar.week=zzc_RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);//»ñÈ¡ÐÇÆÚ
	LOG("year=%d\r\n month=%d\r\n date=%d\r\n hour=%d\r\n min=%d\r\n sec=%d\r\n week=%d",calendar.w_year,calendar.w_month,calendar.w_date,calendar.hour,calendar.min,calendar.sec,calendar.week);
//	LOG("year=%d",calendar.w_year);
//	LOG("month=%d",calendar.w_month);
//	LOG("date=%d",calendar.w_date);
//	LOG("hour=%d",calendar.hour);
//	LOG("min=%d",calendar.min);
//	LOG("sec=%d",calendar.sec);
//	LOG("week=%d",calendar.week);


	 lj_time_t time_t;
	time_t.year=(unsigned short)calendar.w_year;
	time_t.month=(unsigned char)calendar.w_month;
	time_t.day=(unsigned char)calendar.w_date;
	time_t.hour=(unsigned char)calendar.hour;
	time_t.minute=(unsigned char)calendar.min;
	time_t.second=(unsigned char)calendar.sec;
	time_t.week=(unsigned char)calendar.week;
	lj_usend_time_1(time_t);


	return 0;
}

//void zzc_sntp_read_timer(lj_time_t * time_st)
//{
//	unsigned int time = sntp_get_current_timestamp();
//	LOG("time:%d\r\n",time);
//	zzc_sntp_get_real_time(time,time_st);
//}
//
//void zzc_set_time(lj_time_t * time_st,unsigned int timestamp)
//{
//	// zzc_sntp_get_real_time(timestamp,time_st);
//	LOG("lj need to change timestamp to rtc is =%d",timestamp);
//	RTC_Get(timestamp,time_st);
//}

unsigned char  need_send_get_time=0;
lj_time_t all_time_t;


uint64_t lj_ntp_time_ms = 0;
uint64_t lj_basic_time = 0;//aos_now_ms();

#ifdef _USE_TIME_ZONE_

unsigned char lj_time_zone=8+12;

unsigned char  lj_read_time_zone(unsigned char *zone)
{
	unsigned char buf[4]={0};
	int len=4;
	if(aos_kv_get("lj_time_zone",buf,&len)==0)
	{
		*zone=buf[0];
		ZZC_LOG("lj_read_time_zone ok\r\n");
		return 1;
	}
	else
	{
		ZZC_LOG("lj_read_time_zone wrong\r\n");
//		buf[0]=8+12;
//		aos_kv_set("lj_time_zone" ,buf,len,1);
		return 0;
	}

}

unsigned char lj_set_time_zone(unsigned char zone)
{

	unsigned char buf[4]={0};
	int len=4;
	if(zone>24)
	{
		return 0;
	}
	buf[0]=zone;
	if(aos_kv_set("lj_time_zone" ,buf,len,1)==0)
	{
		ZZC_LOG("lj_set_time_zone ok\r\n");
		return 1;
	}
	else
	{
		ZZC_LOG("lj_set_time_zone wrong\r\n");
		return 0;
	}
}


#endif// _USE_TIME_ZONE_

void time_cb( const char *ntp_offset_time_ms)
{
	unsigned int len=0;
	unsigned int  tx=0;
	uint64_t tx_ms=0;


	// usart_log("ntp_offset_time_ms=%s",ntp_offset_time_ms);

	len=strlen(ntp_offset_time_ms);

	 while (len -- > 0) {
		 tx_ms *= 10;
		 tx_ms += ntp_offset_time_ms[0] - '0';
		   ntp_offset_time_ms ++;
		}
	tx=tx_ms/1000;

//	 len=strlen(ntp_offset_time_ms);
//	 	if(len>=3)
//	 	{
//	 		len=len-3;
//	 	}
//	 	 while (len -- > 0) {
//	 	        tx *= 10;
//	 	        tx += ntp_offset_time_ms[0] - '0';
//	 	       ntp_offset_time_ms ++;
//	 	    }

#ifdef 	_USE_TIME_ZONE_

		if((lj_time_zone>=12)&&(lj_time_zone<=24))
		{
			RTC_Get(tx+3600*(lj_time_zone-12));
			lj_ntp_time_ms=tx_ms+3600*(lj_time_zone-12)*1000;
		}
		else if(lj_time_zone<12)
		{
			if((tx<12*3600)||(tx_ms<3600*1000*12))
			{
				ZZC_LOG("ntp_offset_time_ms wrong \r\n");
				return;
			}
			else
			{
				RTC_Get(tx-3600*(12-lj_time_zone));
				lj_ntp_time_ms=tx_ms-3600*(12-lj_time_zone)*1000;
			}
		}
		else
		{
			ZZC_LOG("wrong lj_time_zone\r\n");
			RTC_Get(tx+3600*0);
			lj_ntp_time_ms=tx_ms+3600*0*1000;
		}

#else

	 	RTC_Get(tx+3600*8);

	 	lj_ntp_time_ms=tx_ms+3600*8*1000;
#endif //_USE_TIME_ZONE_
	 	lj_basic_time=aos_now_ms();
}

void lj_updata_time(void)
{
	uint64_t time_buf=0;
	if(lj_ntp_time_ms==0)
	{
		return ;
	}
	time_buf=aos_now_ms();
	ZZC_LOG("lj_ntp_time_ms=%13d,aos_now_ms() =%d,lj_basic_time=%d",lj_ntp_time_ms,time_buf,lj_basic_time);
	RTC_Get((lj_ntp_time_ms + aos_now_ms() - lj_basic_time)/1000);
}


extern void user_ask_time(void);

#ifdef _USE_SELF_TIME_
void ljhf_get_time(lj_time_t *time_t)
{

	lj_updata_time();
	ZZC_LOG("get in to ljhf_get_time");
}

#else

void ljhf_get_time(lj_time_t *time_t)
{
//	need_send_get_time=1;
//	zzc_set_time(time_t);
	//lj_usend_time_2();
	user_ask_time();
	ZZC_LOG("get in to ljhf_get_time");
}

#endif //_USE_SELF_TIME_

uart_dev_t zzc_uart;
// #define zzc_uart uart_0
unsigned char data_get[256]={0};
unsigned int data_len=0;

static int send_time_count=0;

unsigned short ljhf_usend(unsigned char *buf,unsigned short buf_len)
{
	//return hfilop_uart_send_data(buf,buf_len);
	return hal_uart_send(&zzc_uart,buf,buf_len,10);

}


extern unsigned int time_keep_update;
extern unsigned int bgl_change_times;

#define U_SEND_DIV_NUM (4)//50

unsigned char u_send_div=U_SEND_DIV_NUM;

void timer1_timeout_handler(uint32_t id)//50ms
{
	unsigned char get_len=0;
		unsigned char buf=0;
		int ret=0;
//		LOG("zzc........get in to lj_u_s_deal  \r\n",get_len);
#ifdef _0319_
		get_len = Kfifo_Get(&ring_u_data, &buf, 1);
		Kfifo_Add_Out(&ring_u_data,get_len);
		if(get_len)
		{
			LOG("zzc........get in to lj_u_s_deal  and get_len=%d  \r\n",get_len);
			ljhf_usend(&buf,get_len);
		}
#elif defined _make_out_use_ring_
#ifdef		_use_caipu_
	if(caipu_is_running()==0)
	{
		get_feedback=1;
		u_send_div=U_SEND_DIV_NUM;
	}
#else
	get_feedback=1;
	u_send_div=U_SEND_DIV_NUM;
#endif //_use_caipu_
	u_send_div++;
	if(u_send_div>=U_SEND_DIV_NUM)
	{
		u_send_div=0;
		if(send_ring(get_feedback)==1)
		{
			get_feedback=0;
		}
	}

	restart_loop();
	
#endif

	if(time_keep_update)
	  {
		  time_keep_update--;
		  if(time_keep_update%10==0)
		  {
			  ZZC_LOG("time_keep_update=%d",time_keep_update);
		  }
	  }

/*
	  if(bgl_change_times<600)
	  {
		  bgl_change_times++;
		  if(bgl_change_times%10==0)
		  {
			  ZZC_LOG("bgl_change_times=%d",bgl_change_times);
		  }
	  }
*/
//		aos_post_delayed_action(50, lj_u_s_deal, NULL);
}


uint64_t time_now_100ms=0;
uint64_t time_prev_100ms=0;

static uint64_t user_update_100ms(void)
{
    static uint64_t time_start_100ms = 0;

    if (time_start_100ms == 0) {
        time_start_100ms = HAL_UptimeMs();
    }

    return (HAL_UptimeMs() - time_start_100ms) / 100;
}

extern unsigned int time_query_send_net;


void lj_u_thread(void *params)
{
	int ret=0;
	static int between=0;
	static int read_count=0;
#ifdef _use_caipu_
	unsigned char caipu_wifi_up_buf[11]={0};
	unsigned char caipu_wifi_up_len=11;
//	lj_usart_pkt abuf_st;
#endif // _use_caipu_

  //  sample_context_t *sample_ctx = &g_sample_context;
	ZZC_LOG("zzc................get in to lj_u_thread %s\r\n",__TIME__);
    // aos_post_delayed_action(100, lj_u_deal, NULL);
    //  aos_task_exit(0);
    while (1) {
        // LOG("net_config=%d\r\n",aick_wlan_get_netconfig_state());
		between++;
		if(between>=1)
		{
			between=0;
			#if defined bigualu || defined self
			ret=hal_uart_recv_II(&zzc_uart, data_get, 128,&data_len, 20);
			#else
			ret=hal_uart_recv_II(&zzc_uart, data_get, 1,&data_len, 10);
			#endif
			if(data_len==0)
			{
//				ZZC_LOG("zzc................can read 0");
				// ZZC_LOG("zzc................can read usart,ret=%d,data_len=%d\r\n",ret,data_len);
			//	HAL_SleepMs(100);
#if defined bigualu || defined self
//				time_get_deal_loop();

			#endif
				goto exit;
			}
#if defined bigualu || defined self
			ZZC_LOG("zzc................can read usart,ret=%d,data_len=%d\r\n",ret,data_len);
			lj_usart_get_deal(data_get,data_len);
			#else
			lj_usart_get_deal(data_get,1);
			if(data_get[0]==0xff)
			{
				data_len=0;
				ret=hal_uart_recv_II(&zzc_uart, data_get, 2,&data_len, 30);
				if(data_len==2)
				{
					read_count=data_get[0]*256+data_get[1];
					lj_usart_get_deal(data_get,data_len);
					ZZC_LOG("zzc................read_count=%d\r\n",read_count);
					if(read_count==0)
					{

						read_count=10;
						goto exit;
					}
					data_len=0;
					//20200108
					if(read_count>250)
					{
						ret=hal_uart_recv_II(&zzc_uart, data_get, 250,&data_len, 100);
						ZZC_LOG("zzc................can read usart,ret=%d,data_len=%d\r\n",ret,data_len);
						lj_usart_get_deal(data_get,data_len);
						ret=hal_uart_recv_II(&zzc_uart, &data_get[250], read_count-1-250,&data_len, 100);
						ZZC_LOG("zzc................can read usart,ret=%d,data_len=%d\r\n",ret,data_len);
						lj_usart_get_deal(&data_get[250],data_len);
					}
					else
					{
					ret=hal_uart_recv_II(&zzc_uart, data_get, read_count-1,&data_len, 200);
					ZZC_LOG("zzc................can read usart,ret=%d,data_len=%d\r\n",ret,data_len);
					lj_usart_get_deal(data_get,data_len);
					}
					if((data_get[0]==0x85)||(data_get[0]==0x86))
					{
						HAL_SleepMs(700);
					}
				}
				else
				{
					lj_usart_get_deal(data_get,data_len);
				}
			}
			else
			{
				read_count=10;
				ret=hal_uart_recv_II(&zzc_uart, data_get, read_count-1,&data_len, 10);
				lj_usart_get_deal(data_get,data_len);
			}
			#endif//bigualu
		}


		#if 0
		ret=hal_uart_recv_II(&zzc_uart, data_get, read_count,&data_len, 10);
		if(data_len!=0)
		{
			ZZC_LOG("zzc................can read usart,ret=%d,data_len=%d\r\n",ret,data_len);
			lj_usart_get_deal(data_get,data_len);
		}
		#endif
	exit:
		// between++;
		// if(between>=2)
		// {
			timer1_timeout_handler(1);
			hal_wdg_reload(&lj_wdg);
//			time_get_deal_loop();
			// between=0;
		// }
		send_time_count++;
		if(send_time_count>10*15)
		{
			send_time_count=0;
//			lj_updata_time();
		//	lj_usend_time_2();
		}
		restart_loop();

		time_now_100ms = user_update_100ms();
		if (time_prev_100ms == time_now_100ms) {
//			continue;
		}
		else
		{
//			if(time_query_send_net)
//			{
//				time_query_send_net--;
//				if(time_query_send_net==0)
//				{
//					lj_usend_wifi_net_status(all_wifi,all_net);
//				}
//			}
			if(time_can_up)
			{
				time_can_up--;
				if(time_can_up%50==0)
				{
					ZZC_LOG("time_can_up=%d\r\n",time_can_up);
				}
				if(time_can_up==0)
				{
					ZZC_LOG("time_can_up==0\r\n");
				}
			}
//			air_send_zhufan_time_loop();
			lj_time_can_push++;
			time_can_up_dingshi++;
			if(time_can_up_dingshi==time_can_up_dingshi_set)
			{
				ZZC_LOG("lj_time_can_push pass ..............\r\n");
			}
			if(lj_time_can_push==LJ_TIME_CAN_PUSH_ORI)
			{
				ZZC_LOG("lj_time_can_push get LJ_TIME_CAN_PUSH_ORI\r\n");
			}

#ifdef _use_caipu_
			if(get_caipu_change())
			{
				reset_caipu_change();
				caipu_change_buf_fill(caipu_wifi_up_buf,&caipu_wifi_up_len);
				ljhf_publish_point_npush_raw_85(caipu_wifi_up_buf,caipu_wifi_up_len);
			}

			caipu_loop_fun();
#endif //_use_caipu_

			time_prev_100ms=time_now_100ms;
		}

		// LOG("zzc................get in to lj_u_thread %s\r\n",__TIME__);
		HAL_SleepMs(80);
    }
}

#if 0
#include <errno.h>
#include <fcntl.h>

#include <netmgr.h>
//#include <lwip/def.h>
//#include <lwip/netdb.h>
//#include <lwip/sockets.h>
#include <aos/network.h>
//#include <sys/socket.h>
//#include "sys_ipaddr.h"
#define BUFFER_MAX_SIZE  512

//int lj_user_tcp_post_raw_data(unsigned char *buf,unsigned short len)
//{
//
//}

extern unsigned char tcp_up_send_ring(unsigned char *out,unsigned char *len);

static int networktestcmd_tcp_client(void *params)
{
    int  ret = 0;
    int  readlen = 0;
    int  fd = 0;
    int  time = 0;
    int  testtimes = 1;
    char *pbuf = NULL;
    char *pcipaddr = NULL;
    char *pcdestport = NULL;
    char *pcdata = NULL;
    char *pctesttime = NULL;
    struct sockaddr_in addr;
    struct timeval timeout;

    unsigned char send_buf[96]={0};
    unsigned char send_len=0;

    pcipaddr = "120.55.55.127";
    pcdestport = "2051";
//    pcdata = argv[4];

//    if (argc == 6){
//        pctesttime = argv[5];
//        testtimes = atoi(pctesttime);
//        if (0 == testtimes){
//            printf("invalid input tcp client test time %s \r\n", pctesttime);
//            return -1;
//        }
//    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_port = htons((short)atoi(pcdestport));

    if (0 == addr.sin_port){
        printf("invalid input port info %s \r\n", pcdestport);
        return -1;
    }

    addr.sin_addr.s_addr = inet_addr(pcipaddr);

    if (IPADDR_NONE == addr.sin_addr.s_addr){
        printf("invalid input addr info %s \r\n", pcipaddr);
        return -1;
    }

    addr.sin_family = AF_INET;

    fd = socket(AF_INET,SOCK_STREAM,0);
    if (fd < 0){
        printf("fail to creat socket errno = %d \r\n", errno);
        return -1;
    }

    timeout.tv_sec = 30;
    timeout.tv_usec = 0;

    if (setsockopt (fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
                    sizeof(timeout)) < 0) {
        printf("setsockopt failed, errno: %d\r\n", errno);
        goto err;
    }

    if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
        printf("Connect failed, errno = %d, ip %s port %s \r\n", errno, pcipaddr, pcdestport);
        goto err;
    }

    pbuf = aos_malloc(BUFFER_MAX_SIZE);
    if (NULL == pbuf){
        printf("fail to malloc memory %d at %s %d \r\n", BUFFER_MAX_SIZE, __FUNCTION__, __LINE__);
        goto err;
    }

    while(1){

    	if(tcp_up_send_ring(send_buf,&send_len))
    	{
    		if ((ret = send(fd, send_buf, send_len, 0)) <= 0) {
    		            printf("send data failed, errno = %d. for the %d time \r\n", errno, time);
    		            goto err;
    		        }
    	}

        // send-recv


        memset(pbuf, 0, BUFFER_MAX_SIZE);

        readlen = read(fd, pbuf, BUFFER_MAX_SIZE - 1);
        if (readlen < 0){
            printf("recv failed, errno = %d.\r\n", errno);
            goto err;
        }

        if (readlen == 0){
            printf("recv buf len is %d \r\n", readlen);
            break;
        }

        printf("recv server %d time reply len %d \r\n", time, readlen);
        HAL_SleepMs(200);
//        time++;
//
//        if (time >= testtimes){
//            break;
//        }
    }
    close(fd);
    aos_free(pbuf);
    return 0;
err:
    close(fd);
    if (NULL != pbuf){
        aos_free(pbuf);
    }
    return -1;
}

#endif //0


static aos_task_t task_zzc_usart;
static aos_task_t task_zzc_tcp;



//uart_dev_t   zzc_uart;
int lj_usart_init(void)
{

	/* wdg port set */
    lj_wdg.port = WDG_PORT_NUM;

    /* set reload time to 1000ms */
    lj_wdg.config.timeout = 50000; // 10000=/* 1000ms */
	 hal_wdg_init(&lj_wdg);

//		uint8_t pins[] = {4, 3}; // {tx_pin, rx_pin}//5v
		uint8_t pins[] = {2, 1}; // {tx_pin, rx_pin}//3v3
		int ret=0;

		zzc_uart.port = 1;
		zzc_uart.config.baud_rate = 9600;
		zzc_uart.priv = pins;

		/* uart attr config */
		zzc_uart.config.data_width   = DATA_WIDTH_8BIT;
		zzc_uart.config.parity       = NO_PARITY;
		zzc_uart.config.stop_bits    = STOP_BITS_1;
		zzc_uart.config.flow_control = FLOW_CONTROL_DISABLED;
		zzc_uart.config.mode         = MODE_TX_RX;

		hal_uart_init(&zzc_uart);

//		unsigned char bb[]={0x01,0x02};
//		hal_uart_send(&uart_app,bb,2,0);

//		ret=aos_task_new("zzc_usart", lj_u_thread, NULL, 4096);
		ret=aos_task_new_ext(&task_zzc_usart, "zzc_usart", lj_u_thread, NULL, 4096, AOS_DEFAULT_APP_PRI);
#if 0
		ret=aos_task_new_ext(&task_zzc_tcp, "zzc_tcp", networktestcmd_tcp_client, NULL, 3072, AOS_DEFAULT_APP_PRI);
#endif //0


}





#ifdef tianjun
#include <hal/soc/gpio.h>



#define BLUE_IO 0
#define LED_60_IO 12
#define LED_120_IO 11
#define LED_180_IO 2
#define KEY_IO 5
#define HEAT_IO 17//17
#define BUZ_IO 14
#define FAN_IO 1


static gpio_dev_t io_led_60;
static gpio_dev_t io_led_120;
static gpio_dev_t io_led_180;
static gpio_dev_t io_led_blue;
static gpio_dev_t io_key;
static gpio_dev_t io_heat;
static gpio_dev_t io_fan;
static gpio_dev_t io_buz;

void set_zijian2(void);
void buz_once(unsigned int time_ms);

void set_io(gpio_dev_t *st,unsigned char buf)
{
	if(buf==0)
	{
		hal_gpio_output_low(st);
	}
	else
	{
		hal_gpio_output_high(st);
	}
}

unsigned char get_key(void)
{
	uint32_t level;
	hal_gpio_input_get(&io_key, &level);
	return level;
}

#define LJ_TIME_DIV 50
#define LJ_KEY_PRESS (100/LJ_TIME_DIV)
#define LJ_KEY_PRESS_LONG (1000/LJ_TIME_DIV)
#define LJ_KEY_AWSS (5000/LJ_TIME_DIV)

//typedef struct{
//	unsigned char on;
//	unsigned int time;
//	unsigned char fan;
//	unsigned char heat;
//}tianjun_st;

void l60_on(void)
{
	set_io(&io_led_60,0);
}

void l60_off(void)
{
	set_io(&io_led_60,1);
}

void l120_on(void)
{
	set_io(&io_led_120,0);
}

void l120_off(void)
{
	set_io(&io_led_120,1);
}

void l180_on(void)
{
	set_io(&io_led_180,0);
}

void l180_off(void)
{
	set_io(&io_led_180,1);
}

void lblue_on(void)
{
	set_io(&io_led_blue,0);
}

void lblue_off(void)
{
	set_io(&io_led_blue,1);
}


void lfan_on(void)
{
	set_io(&io_fan,1);
}

void lfan_off(void)
{
	set_io(&io_fan,0);
}


void lheat_on(void)
{
	set_io(&io_heat,1);
}

void lheat_off(void)
{
	set_io(&io_heat,0);
}

pwm_dev_t buz_st;

void tian_gopi_init(void)
{
	io_led_60.port = LED_60_IO;
	io_led_120.port = LED_120_IO;
	io_led_180.port = LED_180_IO;
	io_led_blue.port = BLUE_IO;
	io_key.port = KEY_IO;
	io_heat.port = HEAT_IO;
	io_fan.port = FAN_IO;
	io_buz.port = BUZ_IO;

	io_led_60.config = OUTPUT_PUSH_PULL;
	io_led_120.config = OUTPUT_PUSH_PULL;
	io_led_180.config = OUTPUT_PUSH_PULL;
	io_led_blue.config = OUTPUT_PUSH_PULL;

	io_key.config = INPUT_PULL_UP;
	io_heat.config = OUTPUT_PUSH_PULL;
	io_fan.config = OUTPUT_PUSH_PULL;
	io_buz.config = OUTPUT_PUSH_PULL;

    hal_gpio_init(&io_led_60);
    hal_gpio_init(&io_led_120);
    hal_gpio_init(&io_led_180);
    hal_gpio_init(&io_led_blue);
    hal_gpio_init(&io_key);
    hal_gpio_init(&io_heat);
    hal_gpio_init(&io_fan);
    hal_gpio_init(&io_buz);

    buz_st.port=4;//BUZ_IO;
    buz_st.config.duty_cycle=50;
    buz_st.config.freq=2000;
    buz_st.priv=BUZ_IO;
    hal_pwm_init(&buz_st);
    hal_pwm_stop(&buz_st);
//    buz_once(200);

    lblue_off();
	l60_off();
	l120_off();
	l180_off();
	lfan_off();
	lheat_off();
}

void aos_fan_init(void)
{
	hal_gpio_init(&io_fan);
	lfan_off();
}

unsigned char fun=0;
unsigned char fun_time=0;
unsigned char delay_time=0;
unsigned char all_second=0;
unsigned char half_second=0;
unsigned char second=0;
unsigned char minute=0;
unsigned char time_500ms=0;

unsigned char ganyiji_status_buf[4]={0};
unsigned char need_send_status_count=0;
unsigned char ganyiji_send_id=0;
unsigned char time_1s=0;

unsigned char zijian_time=0;

unsigned char fan_delay=0;

unsigned char time_50ms=0;

void tianjun_send_status(void);

void zijian_off(void)
{
	zijian_time=0;
}

void fun_60_init(void)
{
	fun=1;
	fun_time=60;//60
	delay_time=3;
	half_second=0;
	all_second=1;
	second=0;
	zijian_off();
	LOG("do fun 60" );
}

void fun_120_init(void)
{
	fun_60_init();
	fun=2;
	fun_time=120;
	LOG("do fun 120" );
}

void fun_180_init(void)
{
	fun_60_init();
	fun=3;
	fun_time=180;
	LOG("do fun 180" );
}

void off_init(void)
{
	fun=0;
	fun_time=0;
	delay_time=0;
	zijian_off();
	LOG("do fun off" );
}

void ganyiji_tran_deal(unsigned char mode)
{

	switch(mode)
	{
		case 0:
			off_init();
		buz_once(400);
		break;
		case 1:
			fun_60_init();
		buz_once(200);
		break;
		case 2:
			fun_120_init();
		buz_once(200);
		break;
		case 3:
			fun_180_init();
		buz_once(200);
		break;
		default:
			break;
	}
}

void ganyiji_switch_click_handler(void)
{
	//reverse_on_off();
//	buz_once(200);
	switch(fun)
	{
		case 0:
			fun_60_init();
			buz_once(200);
		break;
		case 1:
//			if(delay_time)
//			{
//				fun_120_init();
//			}
//			else
//			{
//				off_init();
//			}
			fun_120_init();
			buz_once(200);
		break;
		case 2:
//			if(delay_time)
//			{
//				fun_180_init();
//			}
//			else
//			{
//				off_init();
//			}
		fun_180_init();
		buz_once(200);
		//	fun=3;
		break;
		case 3:
			off_init();
		buz_once(500);
//		fun_60_init();

		break;
	}

//	ganyiji_status_buf[0]=0x01;
//	ganyiji_status_buf[1]=0x00;
//	ganyiji_status_buf[2]=0x01;
//	ganyiji_status_buf[3]=fun;
//	report_tran_status_1(ganyiji_status_buf,4,ganyiji_send_id,1,0x85);
//	ganyiji_send_id++;
//	if(ganyiji_send_id>100)
//	{
//		ganyiji_send_id=0;
//	}
	need_send_status_count=1;
	time_1s=0;

}

void send_ganyiji_status(unsigned char times)
{
	ganyiji_status_buf[0]=0x01;
	ganyiji_status_buf[1]=0x00;
	ganyiji_status_buf[2]=0x01;
	ganyiji_status_buf[3]=fun;
	LOG("report fun =%d",fun);
//	report_tran_status_1(ganyiji_status_buf,4,ganyiji_send_id,1,0x85,times);
	ganyiji_send_id++;
	if(ganyiji_send_id>100)
	{
		ganyiji_send_id=0;
	}
}

void led_loop(void)
{
	unsigned char l60_buf=0;
	unsigned char l120_buf=0;
	unsigned char l180_buf=0;
//	unsigned char lblue_buf=0;

	if(zijian_time)
	{
		if(zijian_time==3)
		{
			l60_buf=1;
		}
		else if(zijian_time==2)
		{
			l120_buf=1;
		}
		else if(zijian_time==1)
		{
			l180_buf=1;
		}
	}
	else
	{
		switch(fun)
		{
			case 0:
				break;
			case 1:
				if(delay_time==0)
				{
					if(all_second==1)
					{
						l60_buf=1;
					}
				}
				else
				{
					l60_buf=1;
				}
				break;
			case 2:
				if(delay_time==0)
				{
					if(all_second==1)
					{
						l120_buf=1;
					}
				}
				else
				{
					l120_buf=1;
				}
				break;
			case 3:
				if(delay_time==0)
				{
					if(all_second==1)
					{
						l180_buf=1;
					}
				}
				else
				{
					l180_buf=1;
				}
				break;
		}
	}



	if(l60_buf){l60_on();}else{l60_off();}
	if(l120_buf){l120_on();}else{l120_off();}
	if(l180_buf){l180_on();}else{l180_off();}
//	if(lblue_buf){lblue_on();}else{lblue_off();}

}

void work_loop(void)
{
	unsigned char fan_buf=0;
	unsigned char heat_buf=0;
	switch(fun)
	{
		case 0:
			break;
		case 1:
		case 2:
		case 3:
			if(delay_time)
			{
			}
			else
			{
				if(fun_time)
				{
					fan_buf=1;
					heat_buf=1;
				}
				else
				{
					off_init();
					buz_once(400);
				}
			}
		break;
	}

	if(heat_buf)
	{
		lheat_on();
		fan_delay=30;
	}
	else
	{
		lheat_off();
	}
	if(fan_delay)
	{
		fan_buf=1;
	}
	if(fan_buf)
	{
		lfan_on();
	}
	else
	{
		lfan_off();
	}
}

unsigned char up_power=0;
unsigned char send_once=0;
void time_loop(void)
{
//	dbg_uart_print("get in to time_loop\r\n");
	time_50ms++;
	if(time_50ms<10)
	{
		return ;
	}
	time_50ms=0;

	time_1s++;
	if(time_1s>2)
	{
		time_1s=0;
		if((need_send_status_count<3)&&(need_send_status_count>0))
		{
			need_send_status_count--;
			tianjun_send_status();
//		send_ganyiji_status(3);
		}
		else if(need_send_status_count>=3)
		{
			need_send_status_count--;
		}
	}

	if((all_net==1)&&(send_once==0))
	{
		send_once=1;
		tianjun_send_status();
	}






	time_500ms=0;
	if(half_second)
	{
		half_second=0;
		if(all_second)
		{
			all_second=0;
		}
		else
		{
			all_second=1;
		}
		second++;
//		LOG("second=%d",second);
		if(second>=60)//60
		{
			second=0;
			if(fun_time)
			{
				fun_time--;
//				fun_time--;
//				fun_time--;
//				fun_time--;
//				fun_time--;
			}
		}
		if(delay_time)
		{
			delay_time--;
		}
		if(fan_delay)
		{
			fan_delay--;
		}
		if(zijian_time)
		{

			zijian_time--;
		//	if(zijian_time)
		//	buz_once(200);
		}
	}
	else
	{
		half_second=1;
	}
}

void ganyiji_switch_long_press_handler(void)
{
//	TIM_Cmd(TIM2, DISABLE);
	buz_once(400);
	off_init();

}

static aos_timer_t lj_buz_timer;
unsigned char buz_times=0;
unsigned char buz_ms=0;

void buz_cb(void *arg1, void *arg2)
{

	hal_pwm_stop(&buz_st);
	aos_timer_stop(&lj_buz_timer);
	aos_timer_free(&lj_buz_timer);
	LOG("get in to buz_cb");
	if(buz_times)
	{
		buz_times--;
	}

if(buz_times==0)
{

}
else
{
	if(buz_times%2==0)
	{
		aos_timer_new_ext(&lj_buz_timer, buz_cb, NULL, 500, 0, 1);
	}
	else
	{
		aos_timer_new_ext(&lj_buz_timer, buz_cb, NULL, buz_ms, 0, 1);
		hal_pwm_start(&buz_st);
	}
}
//	set_io(&io_buz,1);

}

void buz_once(unsigned int time_ms)
{
	aos_timer_stop(&lj_buz_timer);
	aos_timer_new_ext(&lj_buz_timer, buz_cb, NULL, time_ms, 0, 1);
//	set_io(&io_buz,0);
	hal_pwm_start(&buz_st);
	LOG("get in to buz once.......................................");
	buz_times=1;
//	if(buz_working==0)
//	{
//
//	}
//	else
//	{
//		buz_timer_stop();
//		TIM_Cmd(TIM2, DISABLE);
//	}
//	buz_working=1;
//	plt_timer_change_period(buz_timer, time_ms, 0);
//	buz_timer_start();
//	TIM_Cmd(TIM2, ENABLE);
//	buz_2=0;

}

void buz_twice(unsigned int time_ms)
{
	buz_once(time_ms);
	buz_ms=time_ms;
	buz_times=(2-1)*2+1;
//	if(buz_working==0)
//	{
//
//	}
//	else
//	{
//		buz_timer_stop();
//		TIM_Cmd(TIM2, DISABLE);
//	}
//	buz_working=1;
//	plt_timer_change_period(buz_timer, time_ms, 0);
//	buz_timer_start();
//	TIM_Cmd(TIM2, ENABLE);
//	buz_2=2;
//
}


void tj_zijian_init(void)
{
	if(!get_key())
	{
		aos_msleep(20);
		if(!get_key())
		{
			set_zijian2();
			return ;
		}
	}
//	else
//	{
	zijian_time=3;
	buz_once(200);
//	buz_twice(200);
	l60_on();
//	}

//	l120_on();
//	l180_on();
}


unsigned char tj_zijian_step=0;
void tj_zijian_net_init(void)
{
	tj_zijian_step=1;
}

void tj_zijian_net_loop(void)
{
	while(1)
	{
		switch(tj_zijian_step)
		{
		case 0:
		break;
		case 1:
			l60_on();
			l120_off();
			l180_off();
			lblue_off();
			lfan_on();
			lheat_off();
			aos_msleep(1000);
			tj_zijian_step=2;
			break;
		case 2:
			l60_off();
			l120_on();
			l180_off();
			lblue_off();
			lfan_off();
						lheat_on();
			aos_msleep(1000);
			tj_zijian_step=3;
			break;
		case 3:
			l60_off();
			l120_off();
			l180_on();
			lblue_off();
			lfan_on();
						lheat_off();
			aos_msleep(1000);
			tj_zijian_step=4;
			break;
		case 4:
			l60_off();
			l120_off();
			l180_off();
			lblue_on();
			lfan_off();
						lheat_on();
			aos_msleep(1000);
			tj_zijian_step=5;
			break;
		case 5:
			l60_off();
			l120_off();
			l180_off();
			lblue_off();
			lfan_off();
						lheat_off();
			aos_msleep(1000);
			return;
//			break;
		default:
			return;

		}
	}
}



void lj_key_detect_event_task(void *arg)
{
    int nCount = 0, awss_mode = 0;
    int timeout = LJ_KEY_AWSS;

    while (1) {
    	 if (!get_key()) {
    		 	 if(up_power==1)
    		 	 {
    	            nCount++;
    	            LOG("nCount :%d", nCount);
    		 	 }
    	        } else {
    	        	up_power=1;
    	            if (nCount >= LJ_KEY_PRESS && nCount < timeout) {
    	            	LOG("press");
    	            	ganyiji_switch_click_handler();
    	            }
    	            if (nCount >= LJ_KEY_PRESS_LONG) {
    	                LOG("long press");
//    	                break;
    	                nCount=0;
//    	                if(zijian_time)
//    	                {
//
//    	                }else
//    	                {
    	                ganyiji_switch_long_press_handler();
//    	                }
    	            }
    	            nCount = 0;
    	        }
    	        if (nCount >= LJ_KEY_AWSS ) {
    	        	LOG("reset");
    	        	ljhf_reset_sdk();
    	        }
		time_loop();
		work_loop();
		led_loop();
        aos_msleep(LJ_TIME_DIV);
    }
    aos_task_exit(0);
}

void tianjun_send_status(void)
{
	lj_usart_pkt tbuf_st;
	unsigned char buf[32]={0};
	unsigned char sum=0,i=0;
	tbuf_st.len_h=0;
	tbuf_st.len_l=0x11;
	tbuf_st.cmd=0x85;
	tbuf_st.r_id=0x00;
	tbuf_st.len_num=0x11;

	sum=sum^tbuf_st.len_h;
	sum=sum^tbuf_st.len_l;
	sum=sum^tbuf_st.cmd;
	sum=sum^tbuf_st.r_id;


	buf[0]=0x00;
	buf[1]=0x00;
	buf[2]=0x01;
	if(fun==0)
	{
		buf[3]=0;
	}
	else
	{
		buf[3]=1;
	}

	buf[4]=0x01;
	buf[5]=0x00;
	buf[6]=0x01;
	buf[7]=fun_time;

	buf[8]=0x02;
	buf[9]=0x00;
	buf[10]=0x01;
	buf[11]=fun;
	for(i=0;i<12;i++)
	{
		sum=buf[i]^sum;
	}
	tbuf_st.check_num=sum;

	ljhf_publish_point_npush_raw(buf,12, tbuf_st);
}

void lj_tianjun_control(unsigned char*payload,unsigned int len )
{
		if(payload[3]==0x87)
        {
			return ;
        }
        else
        {
        	if(payload[5]==0x00)
        	{
        		if(payload[8]==0x01)
        		{
        			fun_60_init();
        		}
        		else if(payload[8]==0x00)
        		{
        			off_init();
        		}

        	}
        	else if(payload[5]==0x02)
        	{
        		if(payload[8]==0x00)
        		{
        			off_init();
        		}
        		else if(payload[8]==0x01)
        		{
        			fun_60_init();
        		}
        		else if(payload[8]==0x02)
				{
        			fun_120_init();
				}
        		else if(payload[8]==0x03)
				{
        			fun_180_init();
				}
        	}
        	else if(payload[5]==0x01)
			{
				if(payload[8]==0x00)
				{
					off_init();
				}
				else if(payload[8]==60)
				{
					fun_60_init();
				}
				else if(payload[8]==120)
				{
					fun_120_init();
				}
				else if(payload[8]==180)
				{
					fun_180_init();
				}
			}
        }
		buz_once(200);
		tianjun_send_status();
}



#endif

void lj_test(unsigned char *buf,unsigned int len)
{
#ifdef tianjun
	ZZC_LOG("get in to lj_test,buf[0]=%02x,buf[1]=%02x,len=%d",buf[0],buf[1],len);
	switch(buf[0])
	{
	case 0:
		set_io(&io_led_60,buf[1]);
		break;
	case 1:
			set_io(&io_led_120,buf[1]);
			break;
	case 2:
			set_io(&io_led_180,buf[1]);
			break;
	case 3:
			set_io(&io_led_blue,buf[1]);
			break;
	case 4:
			set_io(&io_heat,buf[1]);
			break;
	case 5:
			set_io(&io_fan,buf[1]);
			break;
	case 6:
			set_io(&io_buz,buf[1]);
			break;
	}
#endif
}




void demo_pkt_st_init(void)
{
	lj_usart_pkt_st buf;
	buf.ask_net=ljhf_ask_net;
	buf.reset_sdk=ljhf_reset_sdk;
	buf.smart_link=ljhf_smart_link;
	buf.restart=ljhf_restart;
	buf.reload=ljhf_reload;
	buf.get_mac=ljhf_get_mac;
	buf.get_idkey=ljhf_get_idkey;
	buf.set_idkey=ljhf_set_idkey;
	buf.delayms=ljhf_delayms;
	buf.get_time=ljhf_get_time;
	buf.get_ver=ljhf_get_ver;
	buf.publish=ljhf_publish;

	buf.get_ssid_pwd=ljhf_get_ssid_pwd;
	buf.set_ssid_pwd=ljhf_set_ssid_pwd;

	buf.publish_point_npush_raw=ljhf_publish_point_npush_raw;
	buf.publish_point_npush_raw_85=ljhf_publish_point_npush_raw_85;

	buf.publish_point_npush=ljhf_publish_point_npush;

	buf.publish_point_push=ljhf_publish_point_push;
	buf.init_point_all=ljhf_init_point_all;
	buf.unfind=ljhf_unfind;
	buf.unbind=ljhf_unbind;
	buf.set_dis=ljhf_set_dis;
	buf.usend=ljhf_usend;
	buf.get_id_work=ljhf_get_id_work;

	buf.test=lj_test;

//	buf.set=lj_set;

	lj_usart_pkt_st_init(&buf);
//	 notify_init();
//	Kfifo_Init(&ring_buf_data, Q_Put_Inbuf, QUEUE_BUF_LEN);
	if(all_wifi==0)
	{
	all_wifi=2;
	}
	all_net=0;
	lj_usend_wifi_net_status(all_wifi,all_net);
#ifdef tianjun
	tian_gopi_init();
	tj_zijian_init();
#endif
}



#ifdef _zijian2_
//#include "CheckSumUtils.h"
//#include "platform_toolchain.h"
#include <hal/hal.h>


#ifndef SERIAL_NUMBER
#define SERIAL_NUMBER       "UNDF.0000.0000"
#endif

#ifndef LIB_VERSION
#define LIB_VERSION "MOC108 1.0"
#endif

 char *lj_serialnumber(void)
{
    return SERIAL_NUMBER;
}

//static void micoNotify_ApListCallback( ScanResult *pApList )
//{
//    int i = 0;
//    char str[48];
//    unsigned char get_test2=0;
//
//    if ( pApList->ApNum <= 0) {
//            ZZC_LOG("Scan AP Fail");
////#if defined  _chaoren_jhq_ || defined _meizhi_
//#ifdef special
//    		lj_usend_zijian(2);
//#else
//    		lj_usend_zijian(0);
//#endif
//            return;
//        }
// //   ZZC_LOG( "Scan AP Success:\r\n" );
//    for ( i = 0; i < pApList->ApNum; i++ ) {
//        snprintf(str, 48, "  SSID: %s, RSSI: %d\r\n", pApList->ApList[i].ssid, pApList->ApList[i].rssi);
//        ZZC_LOG("%s", str );
//        if(memcmp(pApList->ApList[i].ssid,"linkjob_test2",strlen("linkjob_test2"))==0)
//		{
////#if defined  _chaoren_jhq_ || defined _meizhi_
//#ifdef special
//    		lj_usend_zijian(1);
//#else
//    		lj_usend_zijian(2);
//#endif
//			get_test2=1;
//		}
//    }
//    if(get_test2==0)
//	{
////#if defined  _chaoren_jhq_ || defined _meizhi_
//#ifdef special
//    		lj_usend_zijian(1);
//#else
//    		lj_usend_zijian(0);
//#endif
//	}
//	ljhf_delayms(200);
//	ljhf_restart();
//}


//static void lj_qc_scan(void)
//{
////	/* Register user function when wlan scan is completed */
////	    mico_system_notify_register( mico_notify_WIFI_SCAN_COMPLETED, (void *) micoNotify_ApListCallback, NULL );
////
////	    micoWlanStartScan();
////
////	    mico_rtos_delay_milliseconds(2000);
//	/* Register user function when wlan scan is completed */
//	    mxos_system_notify_register( mxos_notify_WIFI_SCAN_COMPLETED, (void *) mxosNotify_ApListCallback, NULL );
//
//	    mwifi_scan( NULL );
//
//	    mos_msleep(2000);
//}


 static void lj_qc_scan_completed_event(hal_wifi_module_t *m,
                                         hal_wifi_scan_result_t *result,
                                         void *arg)
 {
    int i = 0;
    signed char power=0;
    int power1=0;
    char str[48];
    unsigned char get_test2=0;
    if ( result->ap_num <= 0) {
                ZZC_LOG("Scan AP Fail");
    //#if defined  _chaoren_jhq_ || defined _meizhi_
    #ifdef special
        		lj_usend_zijian(2);
    #else
        		lj_usend_zijian(0);
    #endif
                return;
            }
    ZZC_LOG( "Scan AP Success:\r\n" );
    for ( i = 0; i < result->ap_num; i++ ) {
    	power=result->ap_list[i].ap_power;
    	power1=-60;
        snprintf(str, 48, "  SSID: %s, RSSI: %d\r\n", result->ap_list[i].ssid,power);
        ZZC_LOG("%s",str);
        if(memcmp(result->ap_list[i].ssid,"linkjob_test2",strlen("linkjob_test2"))==0)
        		{
        //#if defined  _chaoren_jhq_ || defined _meizhi_
        	if( power>-60)
        	{
        		ZZC_LOG("rssi is stronger than -60......");
        #ifdef special
            		lj_usend_zijian(1);
        #else
            		lj_usend_zijian(2);
#ifdef tianjun
            	tj_zijian_net_init();
            	tj_zijian_net_loop();
            	zijian_off();
#endif
        #endif
        			get_test2=1;
        	}
        	else
        	{
        		ZZC_LOG("rssi is weaker than -60......");
#ifdef special
           		lj_usend_zijian(1);
       #else
           		lj_usend_zijian(3);
#ifdef tianjun
           	tj_zijian_net_init();
           	tj_zijian_net_loop();
           	zijian_off();
#endif
       #endif
           		get_test2=1;
        	}
        		}
    }
    if(get_test2==0)
   	{
   //#if defined  _chaoren_jhq_ || defined _meizhi_
   #ifdef special
       		lj_usend_zijian(1);
   #else
       		lj_usend_zijian(0);
#ifdef tianjun
            	zijian_off();
#endif
   #endif
   	}
    ljhf_delayms(200);
#ifdef tianjun
#else
//    	ljhf_restart();//tuya
#endif
}

 static const hal_wifi_event_cb_t lj_g_wifi_hal_event = {
     .connect_fail        = NULL,
     .ip_got              = NULL,
     .stat_chg            = NULL,
     .scan_compeleted     = lj_qc_scan_completed_event,
     .scan_adv_compeleted = NULL,
     .para_chg            = NULL,
     .fatal_err           = NULL,
 };

void lj_qc_scan( void )
{
    /* Register user function when wlan scan is completed */
//    mxos_system_notify_register( mxos_notify_WIFI_SCAN_COMPLETED, (void *) mxosNotify_ApListCallback, NULL );
//
//    mwifi_scan( NULL );
//
//    mos_msleep(2000);
	 hal_wifi_module_t *module;

	    module = hal_wifi_get_default_module();
	    hal_wifi_install_event(module, &lj_g_wifi_hal_event);
	    hal_wifi_start_scan(module);
}

#define XTX_FLASH_ID 0x0B4015
#define XTX_FLASH_LOCK 9
#define MX_FLASH_ID 0xC22315
#define MX_FLASH_LOCK 1

static char *lj_boot_ver(void)
{
//	return mxos_get_bootloader_ver();
	return "1";
//	static char boot_ver[64];
//	  uint16_t reg_status;
//	  uint8_t blocks;
//	  uint8_t lock;
//	  uint32_t id;
//
//	  snprintf(boot_ver, sizeof(boot_ver)-3, "MOC108-%s", (const char *)0x00000020);
//
//	  flash_ctrl(0xe240000 + 6, &reg_status);
//	  blocks = (reg_status >> 2) & 0x0F;
//
//	  flash_ctrl(0xe240000 + 14, &id);
//	  ZZC_LOG("flash id = %08x\r\n", id);
//	  if(XTX_FLASH_ID == id)
//	  {
//	      lock = XTX_FLASH_LOCK;
//	  }
//	  else if(MX_FLASH_ID == id)
//	  {
//	      lock = MX_FLASH_LOCK;
//	  }
//	  else
//	  {
//	      lock = blocks + 1;
//	  }
//
//	  if(blocks != lock)
//	  {
//	      strcat(boot_ver, "-x");
//	  }
//
//	  return boot_ver;
}

//extern char *aos_get_app_version(void);
static char *lj_app_ver(void)
{
//    return aos_get_app_version();
	return "no ";
}

static char *lj_lib_ver(void)
{
    return LIB_VERSION;
}

static char *lj_chip_mac(void)
{
    unsigned char mac[6];
    static char macstr[18];
//    mwifi_get_mac( mac);
    hal_wifi_get_mac_addr(NULL, mac);
    sprintf(macstr, "%02X-%02X-%02X-%02X-%02X-%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    return macstr;
}

static void lj_qc_show_ilop(void)
{
//#define PRODUCT_KEY_MAXLEN (20)
//#define DEVICE_NAME_MAXLEN (32)
//#define DEVICE_SECRET_MAXLEN (64)

    char pk[PRODUCT_KEY_MAXLEN + 1] = {0};
    char ps[DEVICE_SECRET_MAXLEN + 1] = {0};
    char dn[DEVICE_NAME_MAXLEN + 1] = {0};
    char ds[DEVICE_SECRET_MAXLEN + 1] = {0};

    HAL_GetProductKey(pk);
    HAL_GetDeviceName(dn);
    HAL_GetDeviceSecret(ds);
    HAL_GetProductSecret(ps);

    if (!strlen(pk))
        return;

    ZZC_LOG("  ILOP: %s,%s,%s,%s\r\n", pk, ps, ds, dn);
}

static void lj_qc_id(void)
{
    ZZC_LOG("ID List: \r\n");
    lj_qc_show_ilop();
}

static uint16_t lj_app_crc(void)
{
//	hal_logic_partition_t *partition_flash = mhal_flash_get_info( HAL_PARTITION_APPLICATION );
//	    unsigned char *mfgbuf = malloc( 1024 );
//	    uint16_t crc = 0;
//	    uint32_t flash_addr = 0x0;
//	    int flash_len = partition_flash->partition_length;
//
//
//	    CRC16_Context mfg_context;
//	    CRC16_Init( &mfg_context );
//
//	    while ( flash_len > 0 )
//	    {
//	        int buf_len = (flash_len > 1024) ? 1024 : flash_len;
//	        flash_len -= buf_len;
//
//	        hal_flash_read( HAL_PARTITION_APPLICATION, &flash_addr, (unsigned char *) mfgbuf, buf_len );
//	        CRC16_Update( &mfg_context, (unsigned char *) mfgbuf, buf_len );
//	    }
//
//	    CRC16_Final( &mfg_context, &crc );
//	  //  snprintf( str, len, "%04X", crc );
//	    if ( mfgbuf ) free( mfgbuf );
//	    return crc;
	return 0;
}




#ifdef _use_caipu_



void print_setting(void)
{
	unsigned char i=0;
	ZZC_LOG("---lj_setting lj_push_mode=%d, setting_len=%d",lj_push_mode,lj_setting_len);
	for(i=0;i<lj_setting_len;i++)
	{
		ZZC_LOG(" %02x",lj_setting[i]);
	}
	ZZC_LOG("------");
}

void lj_set_load(void)
{
	int len=POINT_ALL_NUM_MAX+1,i=0;
//	lj_setting_len=POINT_ALL_NUM_MAX;
	unsigned char buf[POINT_ALL_NUM_MAX+1]={0};
	ZZC_LOG("lj_set_load is =%d",aos_kv_get("lj_set",buf,&len));
	if(len==0)
	{
		lj_push_mode=LJ_PUSH_MODE_ORI;
		lj_setting_len=0;
	}
	else
	{
		lj_push_mode=buf[0];
		lj_setting_len=len-1;
		for(i=0;i<lj_setting_len;i++)
		{
			lj_setting[i]=buf[i+1];
		}
	}
	print_setting();

}

void lj_set(unsigned char *buf,unsigned char len)
{
	unsigned char i=0;
//	unsigned char mode=0;
	unsigned char out_point_len=0;
	static unsigned char lj_push_mode_old=0xff;
	if(len>POINT_ALL_NUM_MAX+1)
	{
		len=POINT_ALL_NUM_MAX+1;
	}
	if(len<1)
	{
		return ;
	}
//	mode=buf[0];
	out_point_len=len-1;


//	if((len==2)&&(buf[0]==buf[1]))
//	{
//		if(buf[0]==0x01)
//		{
//			aos_kv_del("lj_set");
//		}
//	}
//	else
//	{
//		aos_kv_set("lj_set" ,buf,len,1);
//	}
	if((len==1)&&(buf[0]==0xff))
	{
		aos_kv_del("lj_set");
	}
	else
	{
		aos_kv_set("lj_set" ,buf,len,1);
	}
	ZZC_LOG("---lj_set =");
	for(i=0;i<len;i++)
	{
		ZZC_LOG(" %02x",buf[i]);
	}
	ZZC_LOG("------");
	lj_set_load();
}

#endif //_use_caipu_

void do_zijain(void)
{
//	ZZC_LOG("==== MXCHIP Manufacture Test ====\r\n");
//    ZZC_LOG("Serial Number: %s\r\n", lj_serialnumber());
//    ZZC_LOG("Bootloader Version: %s\r\n", lj_boot_ver());
//    ZZC_LOG("Library Version: %s\r\n", lj_lib_ver());
//    ZZC_LOG("APP Version: %s\r\n", lj_app_ver());
//    ZZC_LOG("APP CRC: %04X\r\n", lj_app_crc());
//    ZZC_LOG("MAC: %s\r\n", lj_chip_mac());
#ifdef tianjun
	buz_twice(200);
	aos_msleep(2000);
#endif
    lj_qc_id();
    lj_qc_scan();
}


void set_zijian2(void)
{
	aos_kv_set("zijian2" ,"2",2,1);
}

void ljhf_set_dis(unsigned char buf)
{
	ZZC_LOG("get in to zijian2\r\n");
//	aos_kv_set("zijian2" ,"2",2,1);
	set_zijian2();
	// micoWlanDisablePowerSave();
//	pkt_st_abuf.restart();
	ljhf_delayms(1000);
	ljhf_restart();

}
#else

void ljhf_set_dis(unsigned char buf)
{
//	need_get_weather=1;
}


#endif



#include "aos/kv.h"
extern unsigned int zhufan_time_min_total;

void lj_load_zhufan_total_time(void)
{
#if 0
	unsigned char buf[4]={0};
	unsigned char len=4;
	if(aos_kv_get("zhufan_time_min_total",buf,&len)==0)
	{
		zhufan_time_min_total=buf[0]*256*256*256+buf[1]*256*256+buf[2]*256+buf[3];
	}
	else
	{
		ZZC_LOG("can_read zhufan_time_min_total,and init\r\n");
		aos_kv_set("zhufan_time_min_total",buf,len,1);
		aos_kv_get("zhufan_time_min_total",buf,&len);
	}
	ZZC_LOG("---zhufan_time_min_total=%d\r\n",zhufan_time_min_total);
#endif //0

}

void lj_set_zhufan_total_time(void)
{
#if 0
	unsigned char buf[4]={0};
	unsigned char len=4;
	buf[0]=zhufan_time_min_total/256/256/256;
	buf[1]=((zhufan_time_min_total/256/256)&0xff);
	buf[2]=((zhufan_time_min_total/256)&0xff);
	buf[3]=zhufan_time_min_total%256;
	aos_kv_set("zhufan_time_min_total",buf,len,1);
	lj_load_zhufan_total_time();
#endif //0
}







